﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using UtilsORM.MySqlData;
using UtilsORM;
using UtilsORM.MySqlData;

namespace ServiceWeb.DBBase
{
    public class DBBsClientRole
    {

        EFYihongIotContext ef = new EFYihongIotContext();
        public bool Add(bs_manage_role model, List<bs_manage_role_menu> rmplist)
        {

            try
            {
                ef.bs_manage_role.Add(model);
                ef.SaveChanges();

                ef.bs_manage_role_menu.AddRange(rmplist);
                ef.SaveChanges();
                return true;
            }
            catch //(Exception ex)
            {

                return false;
            }
            finally
            {

            }


        }

        public bool Edit(bs_manage_role _model, List<bs_manage_role_menu> rmpList)
        {
            bool result = true;
            bs_manage_role model = new bs_manage_role();
            //try
            //{

            model = new MySqlYihongIot().Db.Queryable<bs_manage_role>().Where(c => c.r_guid == _model.r_guid).Select(c => c).First();
            model.r_name = _model.r_name;
            model.r_remark = _model.r_remark;
            model.r_code = _model.r_code;
            model.r_addtime = _model.r_addtime;
            ef.bs_manage_role.Update(model);
            ef.SaveChanges();


            //}
            //catch ////(Exception ex)
            //{
            //    result = false;
            //}

            if (EditPower(rmpList, model) == false)
            {
                return false;
            }
            return true;
        }
        public bool EditPower(List<bs_manage_role_menu> _list, bs_manage_role rolemodel)
        {
            bool result = true;

            bs_manage_role_menu model = new bs_manage_role_menu();
            //try
            //{
            //先删除,查询数据库，如果数据库里面有，但传进来的集合没有，就要删除
            List<bs_manage_role_menu> PeiDataList = new MySqlYihongIot().Db.Queryable<bs_manage_role_menu>().Where(c => c.rm_r_guid == rolemodel.r_guid).ToList();
            for (int i = 0; i < PeiDataList.Count; i++)
            {
                bs_manage_role_menu _listitem = _list.Where(c => c.rm_r_guid == PeiDataList[i].rm_r_guid && c.rm_menu_guid == PeiDataList[i].rm_menu_guid).FirstOrDefault();
                if (_listitem == null)
                {
                    //
                    string sql = "delete from bs_manage_role_menu where rm_r_guid='" + PeiDataList[i].rm_r_guid + "' and rm_menu_guid='" + PeiDataList[i].rm_menu_guid + "' ";
                    new MySqlYihongIot().Db.Ado.ExecuteCommand(sql);

                }
            }

            for (int i = 0; i < _list.Count; i++)
            {

                string _memuGuid = _list[i].rm_menu_guid;
                string _rguid = _list[i].rm_r_guid;
                bs_manage_role_menu peimodel = new MySqlYihongIot().Db.Queryable<bs_manage_role_menu>().Where(c => c.rm_r_guid == _rguid && c.rm_menu_guid == _memuGuid).Select(c => c).First();
                if (peimodel == null)
                {
                    //如果没有添加
                    string sql = "insert into bs_manage_role_menu(rm_Guid,rm_r_guid,rm_menu_guid,rm_add_time,rm_add_cu_guid)";
                    sql += "select '" + Guid.NewGuid() + "','" + _list[i].rm_r_guid + "','" + _list[i].rm_menu_guid + "','" + DateTime.Parse(_list[i].rm_add_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "','" + _list[i].rm_add_cu_guid + "' ";
                    new MySqlYihongIot().Db.Ado.ExecuteCommand(sql);

                }
            }
            //}
            //catch (Exception ex)
            //{
            //    result = false;
            //}
            return result;
        }
        public bool Del(List<bs_manage_role> list)
        {
            foreach (bs_manage_role item in list)
            {
                bs_manage_role delrole = ef.bs_manage_role.Where(c => c.r_guid == item.r_guid).FirstOrDefault();
                ef.bs_manage_role.Remove(delrole);
                ef.SaveChanges();
            }
            return true;// _DAl.Del(list);
        }




    }

}
